layui.use('treetable', function() {
    let layer = layui.layer, //弹层
        table = layui.table, //表格
        form = layui.form,
        $ = layui.jquery,
        treeArray = [];
    layer.ready(function () {
        //加载tree并转换成数组
        $.ajax({
            type : 'get',
            url:'productcatalog/select',
            dataType:"json",
            async:false,
            success : function (da) {
                if(da.status==200 ){
                    let treeTemp=treeToArray(da.data.children);
                    let temp="1,2,3,4"
                    for(let i in treeTemp){

                        if (temp.indexOf(treeTemp[i].id) === -1){
                            treeArray.push(treeTemp[i])
                        }

                    }

                }
            },
        });

    });
    table.render({
        elem: '#treeTable',
        id: 'treeTableId',
        data: treeArray,
        cols: [[
            {checkbox: true,title:'选择', width: 50},
            {field: 'id', hide:true},
            {field: 'code', title: '目录编码', width: 400},
            {field: 'name', title: '产品目录', width: 500},
            {field: 'path', title: '目录路径', width: 500},
            {field: 'pid', hide:true},
            {field: 'remark', hide:true},
        ]],
        limit:Number.MAX_VALUE,
        height: $(document).height() - $('#treeTable').offset().top - 65,
        done: function (res, curr, count) {
        }
    });


    // 树形结构数据转单层数组形式
    function treeToArray(nodes) {
        var r=[];
        if (Array.isArray(nodes)) {
            for (var i=0, l=nodes.length; i<l; i++) {
                r.push(nodes[i]); // 取每项数据放入一个新数组
                if (Array.isArray(nodes[i]["children"])&&nodes[i]["children"].length>0)
                    // 若存在children则递归调用，把数据拼接到新数组中，并且删除该children
                    r = r.concat(treeToArray(nodes[i]["children"]));
                delete nodes[i]["children"]
            }
        }
        return r;
    }



    $("#choose").on("click",function () {
        let treeData =   table.checkStatus("treeTableId").data;
        if(treeData.length == 0){
            layer.msg("请选择数据")
        }else{
            let oldData =  parent.table.cache["detailInfoId"];
            let newData = [];
            for(let i in treeData){
                treeData[i].tempId = Number(Math.random().toString().substr(3,3) + Date.now()).toString(36);
                treeData[i].contentCode = treeData[i].code;
                treeData[i].contentName = treeData[i].name;
                treeData[i].contentPath = treeData[i].path;
                treeData[i].contentPid = treeData[i].pid;
                treeData[i].contentId = treeData[i].id;

                newData.push(treeData[i]);
            }
            for(let i in newData){
                delete newData[i].code;
                delete newData[i].name;
                delete newData[i].path;
                delete newData[i].pid;
                delete newData[i].id;

            }

            oldData.push.apply(oldData, newData);
            parent.table.reload('detailInfoId',{data : oldData});
            //执行接口
            layer.msg("保存成功", {icon: 6});
            setTimeout(function () {
                //保存成功则关闭此页面
                let index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            },)
        }
    })
    $("#shutdown").on("click",function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })
});
